package com.wyf.cnbeta;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private ListView listView;
    private SwipeRefreshLayout swipeRefresh;
    private IndexAdapter adapter;
    private List<IndexModel.ResultBean.ListBean> list = new ArrayList<>();
    private RequestQueue queue ;
    private int page =1;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_DUM_NORMAL);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        queue = Volley.newRequestQueue(this);

        initView();
        initSwipe();
        loadData(page);
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.list_view);
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        progressBar = (ProgressBar) findViewById(R.id.index_pb);

    }
    private void initSwipe() {
        swipeRefresh.setColorSchemeColors(Color.RED);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page =1;
                loadData(page);

            }
        });
    }

    private void loadData(final int page){
        if (page!=1){
            progressBar.setVisibility(View.VISIBLE);
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://m.cnbeta.com/touch/default/timeline.json?page="+page, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Gson g = new Gson();
                        IndexModel data = g.fromJson(response.toString(),new  TypeToken<IndexModel>(){}.getType());
                        if(data.getState().equals("success")){
                            list.addAll(data.getResult().getList());

                            initListView();
                            progressBar.setVisibility(View.INVISIBLE);

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Main2Activity.this, "请求出错", Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.INVISIBLE);
            }
        });
        queue.add(jsonObjectRequest);
        swipeRefresh.setRefreshing(false);


    }

    private void initListView() {

        if(adapter == null){
            adapter = new IndexAdapter(this,list);
            listView.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
//        listView.setSelection();



        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                switch (i){
                    case SCROLL_STATE_IDLE:
                        if(listView.getLastVisiblePosition()+1 == listView.getCount()){
                            page++;
                            loadData(page);
                        }
                        break ;
                    case SCROLL_STATE_TOUCH_SCROLL:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Main2Activity.this,NewsActivity.class);
                String url = "http://m.cnbeta.com/wap/view/";


                url+= list.get(i).getUrl().replace("/touch/articles/","");
                intent.putExtra("href",url);
                startActivity(intent);

            }
        });

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);

    }
    public void onPause() {
        super.onPause();
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
